<?php

/**
 * This is the model class for table "tbl_alumnos".
 *
 * The followings are the available columns in table 'tbl_alumnos':
 * @property integer $id_alumno
 * @property string $imss
 * @property string $status
 * @property integer $id_carrera
 * @property integer $id_ciclo_escolar
 *
 * The followings are the available model relations:
 * @property TblCarreras $idCarrera
 * @property TblResidencias[] $tblResidenciases
 *	@property TblResidencias[] $tblResidenciases
 */
class Alumnos extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public $nombres;
	public $apellidop;
	public $apellidom;
	public $matricula;
	public function tableName()
	{
		return 'tbl_alumnos';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('numeross', 'required','message'=>'Debe anotar el Número de Seguro Social'),
			array('id_carrera','required','message'=>'Debe seleccionar una carrera' ),

			array('id_carrera', 'numerical', 'integerOnly'=>true),
			array('id_ciclo_escolar', 'numerical', 'integerOnly'=>true),
			array('seguro', 'required', 'message'=>'Seleccione el tipo de seguro social'),
			//array('imss', 'length', 'max'=>45),
			array('status', 'length', 'max'=>1),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_alumno, status, id_carrera', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'alumnoscarrera' => array(self::BELONGS_TO, 'Carreras', 'id_carrera'),
			'tblResidenciases' => array(self::HAS_MANY, 'TblResidencias', 'id_alumno'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_alumno' => 'Id Alumno',
			'seguro' => 'Seguro',
			'numeross'=>'Número Seguro',
			'status' => 'Status',
			'domicilio' => 'Domicilio',
			'ciudad' => 'Ciudad',
			'telefono' => 'Teléfono',
			'id_carrera' => 'Id Carrera',
			'id_ciclo_escolar'=>'ID CiclosEscolares',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_alumno',$this->id_alumno);
		$criteria->compare('imss',$this->imss,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('id_carrera',$this->id_carrera);
		$criteria->compare('id_ciclo_escolar',$this->id_ciclo_escolar);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Alumnos the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function getAlumnosInscritos($limite,$offset)
	{ //alumnos inscritos en el ciclo activo

		
		$condicion= array('activo' =>1 );
		$res=CiclosEScolares::model()->findByAttributes($condicion);
		
		if($res===null)
		{
			return null;

		}
		
		$criteria=new CDbCriteria;
		$criteria->condition='id_ciclo_escolar=:idciclo';
		$criteria->params= array(':idciclo' =>$res->id_ciclo_escolar);

		$alumnosSistema=Alumnos::model()->findAll($criteria);

		$keyInterno= array();
		if($alumnosSistema===null)
		{
			return null;
		}
		
		
		foreach ($alumnosSistema as $row) {
			$keyInterno[]=$row->id_alumno;
			
		}
		$commando = Yii::app()->dbusuarios->createCommand()
            			->select('id_usuario,nombres,apellidop,apellidom,matricula')
            			->from('usuarios')
            			->where(array('in', 'id_usuario', $keyInterno))
            			
            			->limit ($limite,$offset);
            			
    			
   			$registros=array();
			$dr=$commando->query(); 
			$rows=$dr->readAll();
			
			
			foreach ($rows as $row) {
				$item=new Alumnos();
				$item->matricula=$row['matricula'];
				$item->id_alumno=$row['id_usuario'];
				$item->nombres=$row['nombres'];
				$item->apellidop=$row['apellidop'];
				$item->apellidom=$row['apellidom'];

				$registros[]=$item;
			}
			//print_r($registros);
			return $registros;			
	}
	public function getTotalAlumnosInscritos()
	{
		//Total inscritos en un ciclo activo
		$condicion= array('activo' => 1 );
		
		$res=CiclosEscolares::model()->findByAttributes($condicion);
		$cicloActivo=new CiclosEscolares;

		if($res===null)
		{
			return null;

		}
		else
		{
		 $commando = Yii::app()->db->createCommand()
            			->select('count(*) as total')
            			->from('tbl_alumnos')
            			->where('status=:status',array(':status'=>1))
            			->andwhere('id_ciclo_escolar=:idciclo',array(':idciclo' =>$res->id_ciclo_escolar));
            			
         $dr=$commando->query(); 
			$rows=$dr->readAll();
			
			
		return $rows;	
		}
		
	}

}
